﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Ledger
{
    public partial class TransactionDialog : Form
    {
        public Transaction Info
        {
            get
            {
                Transaction t = new Transaction();

                t.Amount = decimal.Parse(textAmount.Text);
                t.Date = dateDate.Value;
                t.Description = textDescription.Text;
                t.IsRefund = checkRefund.Checked;

                return t;
            }
            set
            {
                textAmount.Text = value.Amount.ToString();
                dateDate.Value = value.Date;
                textDescription.Text = value.Description;
                checkRefund.Checked = value.IsRefund;
            }
        }

        public TransactionDialog()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            bool ok = true;

            if (textDescription.Text == "")
            {
                MessageBox.Show("Please enter a description for this transaction.");
                ok = false;
            }
            Regex r = new Regex(Properties.Resources.currencyRegex);
            if (!r.IsMatch(textAmount.Text))
            {
                MessageBox.Show("Please enter a valid amount for this transaction.");
                ok = false;
            }

            if (ok)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
